<?php
include('../includes/common.php');
if ($islogin2 == 1) { } else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>

<?php
$my = isset($_GET['my']) ? $_GET['my'] : null;
if ($my == 'add') {
    $active = 'moneylist-add';
    $title = '添加记录';
} else {
    $active = 'moneylist';
    $title = '变动记录';
}
include('head.php');
?>
<section id="main-content">
    <section class="wrapper">
        <div class="modal fade" align="left" id="search" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h4 class="modal-title" id="myModalLabel">搜索记录</h4>
                    </div>
                    <div class="modal-body">
                        <input type="text" class="form-control" name="kw" placeholder="请输入记录"><br />
                        <button type="button" class="btn btn-primary btn-block" id="search_submit">搜索</button>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
        <?php
        $my = isset($_GET['my']) ? $_GET['my'] : null;
        $uid = $userrow['id'];
        if ($my == 'add') {
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            添加新记录
                        </header>
                        <div class="panel-body">
                            <form action="./moneylist.php?my=add_submit" method="post" class="cmxform form-horizontal tasi-form" role="form">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">宿舍</label>
                                    <div class="col-sm-10">
                                        <select name="sid" class="form-control" >					
 									<?php 
										$rs = $conn->query("select * from dorm_list where uid = '$uid'");
										if($rs&&mysqli_num_rows($rs)){
											while ($res = $rs->fetch_array()) {
											echo "<option value='".$res['id']."'>".$res['name']."</option>";}}
									?>   
									</select>
                                    </div>
                                </div><br />
								<div class="form-group">
                                    <label class="col-sm-2 control-label">类型:</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="type">
                                            <option value="1" selected>进账</option>
                                            <option value="0">消费</option>
                                        </select>
                                    </div>
                                </div><br />
								<div class="form-group">
                                    <label class="col-sm-2 control-label">金额:</label>
                                    <div class="col-sm-10"><input type="text" name="money" value="" class="form-control" required /></div>
                                </div><br />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">备注:</label>
                                    <div class="col-sm-10"><input type="text" name="remarks" value="" class="form-control" /></div>
                                </div><br />

                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-danger" type="submit">确认添加</button>
                                        <a href="./moneylist.php" class="btn btn-default" type="button">返回列表</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
        <?php
        } elseif ($my == 'add_submit') {
            $sid = trim($_POST['sid']);
            $type = trim($_POST['type']);
			$money = trim($_POST['money']);
            $remarks = $_POST['remarks'];
            if ($sid == NULL || $money == NULL) {
                showmsg('保存错误,请确保每项都不为空!', 3);
            } else {
                    $sql = "insert into rmb_list(uid,sid,addtime,type,money,remarks) values('$uid','$sid','$nowDate','$type','$money','$remarks')";
                    $rs = $conn->query($sql);
					$rs1 = $conn->query("select * from dorm_list where id = '$sid'");
					$res = $rs1->fetch_assoc();
					$rmb = $res['money'];
					if($res['state'] == 0){
						showmsg('该宿舍已被封禁', 4);
					}else{
					if($type == 1){
						$zrmb = $rmb + $money;
					}else{
						$zrmb = $rmb - $money;
					}
					$rs = $conn->query("update dorm_list set money='$zrmb' where id='$sid'");
                    if ($rs) {
                        showmsg('添加变动成功！<br/><a href="./moneylist.php">>>返回记录列表</a>', 1);
                    } else {
                        showmsg('添加变动失败！', 4);
                    }}
                
            }
        } elseif ($my == 'edit_submit') {
            $id = $_GET['id'];
            $rs1 = $conn->query("select * from rmb_list where id='$id' and uid = '$uid'");
			$res1 = $rs1->fetch_assoc();
            if ($rs1->num_rows == 0) {
                showmsg('当前记录不存在！', 3);
            } else {
				$oldtype = $res1['type'];
                $oldmoney = $res1['money'];
                $type = trim($_POST['type']);
				$sid = trim($_POST['sid']);
                $money = trim($_POST['money']);
				$remarks = $_POST['remarks'];
                if ($money == NULL ) {
                    showmsg('保存错误,请确保每项都不为空!', 3);
                } else {
					$rs2 = $conn->query("select * from dorm_list where id = '$sid'");
					$res2 = $rs2->fetch_assoc();
					$rmb = $res2['money'];
					if($res2['state'] == 0){
						showmsg('该宿舍已被封禁', 4);
					}else{
					if($oldtype == 1){
						$rmb = $rmb - $oldmoney;
					}else{
						$rmb = $rmb + $oldmoney;
					}
					if($type == 1){
						$zrmb = $rmb + $money;
					}else{
						$zrmb = $rmb - $money;
					}
					$rs = $conn->query("update dorm_list set money='$zrmb' where id='$sid'");
					
                    $rs = $conn->query("update rmb_list set type='$type',money='$money',remarks='$remarks' where id='$id'");
                    if ($rs) {
                        showmsg('修改信息成功！<br/><br/><a href="./moneylist.php">>>返回记录列表</a>', 1);
                    } else {
                        showmsg('修改信息失败！', 4);
                    }
                }}
            }
        } elseif ($my == 'edit') {
            $id = $_GET['id'];
            $rs = $conn->query("select * from rmb_list where id='$id' and uid = '$uid'");
            if ($rs->num_rows == 0) {
                exit("<script language='javascript'>alert('当前记录不存在');history.go(-1);</script>");
            }
            $row = $rs->fetch_assoc();
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            修改记录信息 - 将影响宿舍舍费
                        </header>
						
                        <div class="panel-body">
                            <form action="./moneylist.php?my=edit_submit&id=<?php echo $row['id']; ?>&oldtype=<?php echo $row['type']; ?>&oldmoney=<?php echo $row['money']; ?>" method="post" class="form-horizontal" role="form">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">宿舍</label>
                                    <div class="col-sm-10">
                                      <select name="sid" class="form-control" >					
										<option value="<?php echo $row['sid']; ?>" >
										<?php 					
										$sid = $row['sid'];
										$rs1 = $conn->query("select * from dorm_list where id = '$sid'");
										$res = $rs1->fetch_assoc();
										echo $res['name'];?>
										</option>
									  </select>
                                    </div>
                                </div><br />
								 <div class="form-group">
                                    <label class="col-sm-2 control-label">类型:</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" name="type">
                                            <option value="1" <?= $row['type'] == 1 ? "selected" : "" ?>>进账</option>
                                            <option value="0" <?= $row['type'] == 0 ? "selected" : "" ?>>消费</option>
                                        </select>
                                    </div>
                                </div><br />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">金额:</label>
                                    <div class="col-sm-10"><input type="text" name="money" value="<?php echo $row['money']; ?>" class="form-control" /></div>
                                </div><br />
                               
								<div class="form-group">
                                    <label class="col-sm-2 control-label">备注:</label>
                                    <div class="col-sm-10"><input type="text" name="remarks" value="<?php echo $row['remarks']; ?>" class="form-control" /></div>
                                </div><br />
								

								
								
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-danger" type="submit">确认修改</button>
                                        <a href="./moneylist.php" class="btn btn-default" type="button">返回列表</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>
                </div>
            </div>
        <?php
        } elseif ($my == 'delete') {
            $id = $_GET['id'];
            $rs = $conn->query("select * from rmb_list where id='$id' and uid = '$uid'");
            if ($rs->num_rows == 0) {
                showmsg('当前记录不存在！', 3);
            } else {
                $rs = $conn->query("delete from rmb_list where id='$id'");
                if ($rs) {
                    showmsg('删除成功！<br/><br/><a href="./moneylist.php">>>返回记录列表</a>', 1);
                } else {
                    showmsg('删除失败！', 4);
                }
            }
        } else {
            ?>
            <div class="row">
                <div class="col-lg-12">
                    <section class="panel">
                        <header class="panel-heading">
                            <a href="./moneylist.php?my=add" class="btn btn-primary">添加记录</a>&nbsp;
                            <div class="btn-group">
                                <a class="btn btn-warning" href="#" data-toggle="dropdown">
                                    操作
                                    <i class="icon-angle-down "></i>
                                </a>
                                <ul class="dropdown-menu">
                                    <li><a href="#" onclick="datadel()"><i class="icon-trash"></i>删除</a></li>
                                </ul>
                            </div>
                            
                            <a href="javascript:listTable('start')" class="btn btn-default" title="刷新记录列表"><i class="fa fa-refresh"></i></a>
                        </header>
                        <div id="listTable"></div>
                    </section>
                </div>
            </div>
        <?php
        }
        ?>
    </section>
</section>
<script src="../static/user/js/common-scripts.js"></script>
<script>
    function listTable(query) {
        var url = window.document.location.href.toString();
        var queryString = url.split("?")[1];
        query = query || queryString;
        if (query == 'start' || query == undefined) {
            query = '';
            history.replaceState({}, null, './moneylist.php');
        } else if (query != undefined) {
            history.replaceState({}, null, './moneylist.php?' + query);
        }
        layer.closeAll();
        var ii = layer.load(2, {
            shade: [0.1, '#fff']
        });
        $.ajax({
            type: 'GET',
            url: 'moneylist-table.php?' + query,
            dataType: 'html',
            cache: false,
            success: function(data) {
                layer.close(ii);
                $("#listTable").html(data)
            },
            error: function(data) {
                layer.msg('服务器错误');
                return false;
            }
        });
    }

    function datadel() {
        layer.confirm('您确定要删除这些记录吗？<br>不会影响对应宿舍舍费', {
            btn: ['确定', '取消']
        }, function() {
            var cks = document.getElementsByName("checkid");
            var str = "";
            for (var i = 0; i < cks.length; i++) {
                if (cks[i].checked) {
                    str += cks[i].value + "&";
                }
            }
            str = str.substring(0, str.length - 1);
            $.ajax({
                url: "ajax.php?act=delSelect2",
                type: "POST",
                data: {
                    "str": str
                },
                dataType: "json",
                success: function(result) {
                    if (result.code == 0) {
                        window.location.reload();
                    }
                },
                error: function(data) {
                    layer.msg('服务器错误');
                    return false;
                }
            });
        })
    }


    $(document).ready(function() {
        $("#search_submit").click(function() {
            var kw = $("input[name='kw']").val();
            $("#search").modal('hide');
            if (kw == '') {
                listTable('start');
            } else {
                listTable('kw=' + kw);
            }
        });
    });

    $(document).ready(function() {
        listTable();
    })
</script>